﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GabageFactory.Models;

namespace GabageFactory.Controllers
{
    [HandleError]
    [Authorize]
    public class HomeController : BaseController
    {
        private FactoryReposity factoryReposity = new FactoryReposity();

        private MessageReposity messageReposity = new MessageReposity();
        
        public ActionResult Index()
        {
            ViewData["corpName"] = "";
            User user = GetCurrentUser();
            if (user != null && user.Factory != null)
            {
                ViewData["corpName"] = user.Factory.Name;
            }

            return View();
        }

        public ActionResult Separator()
        {
            return View();
        }

        public ActionResult Top()
        {
            User user = GetCurrentUser();
            ViewData["user"] = user;

            return View();
        }

        [Authorize]
        public ActionResult Message()
        {
            User user = GetCurrentUser();
            IEnumerable<Message> messages = messageReposity.AquireMessages(user.FactoryId, 1);
            Message[] messageArray = new Message[0];
            if (messages != null && messages.Count() > 0)
            {
                messageArray = messages.ToArray();
            }
            ViewData["messages0"] = messageArray;


            IEnumerable<Message> messages1 = messageReposity.AquireMessages(user.FactoryId, 2);
            Message[] message1Array = new Message[0];
            if (messages1 != null && messages1.Count() > 0)
            {
                message1Array = messages1.ToArray();
            }
            ViewData["messages1"] = message1Array;


            IEnumerable<Message> messages2 = messageReposity.AquireMessages(user.FactoryId, 3);
            Message[] message2Array = new Message[0];
            if (messages2 != null && messages2.Count() > 0)
            {
                message2Array = messages2.ToArray();
            }
            ViewData["messages2"] = message2Array;

            return View();
        }

        [Authorize]
        public ActionResult LeftMenu()
        {
            User user = GetCurrentUser();
            ViewData["user"] = user;
            ViewData["factories"] = factoryReposity.ListFactories();
            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult Deny()
        {
            return View();
        }
    }
}
